package com.item.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.*;
import com.item.mall.mapper.CartMapper;
import com.item.mall.mapper.OrderDetailMapper;
import com.item.mall.mapper.ProductMapper;
import com.item.mall.mapper.UserAddressMapper;
import com.item.mall.service.OrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.mall.service.UserAddressService;
import com.item.mall.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    UserAddressMapper userAddressMapper;


    @Override
    public UserAddress addOrderDetail(Integer userId, Orders order) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<Cart> cartList = cartMapper.selectList(queryWrapper);
        for (Cart cart : cartList) {
            orderDetailMapper.insert(new OrderDetail(null,order.getId(),cart.getProductId(),cart.getQuantity(),cart.getCost()));
            productMapper.updateStock(cart.getProductId(),cart.getQuantity());
        }
        cartMapper.delete(queryWrapper);
        return userAddressMapper.selectById(order.getAddressId());
    }
}
